package com.example.homework.utils;

import android.util.Log;

import com.example.homework.entity.NowWeatherInfo;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JsonUtils {

    private static final String TAG = JsonUtils.class.getName();

    public static NowWeatherInfo parseNowWeatherJson(String jsonStr) {
        NowWeatherInfo info = null;
        try {
            JSONObject locationJson = new JSONObject(jsonStr);
            String nowJson = locationJson.optString("now");
            Gson gson = new Gson();
            info = gson.fromJson(String.valueOf(nowJson), NowWeatherInfo.class);
            Log.d(TAG, "parseJsonData: " + nowJson);
        } catch (JSONException e) {
            Log.d(TAG, "parseJsonData: 解析Json数据失败");
        }
        return info;
    }

    public static String parseCityLocationId(String jsonStr){
        JSONObject locationJson = null;
        try {
            locationJson = new JSONObject(jsonStr);
            //String nowJson = locationJson.optString("location");
            JSONArray  jsonArray = locationJson.getJSONArray("location");
            if (jsonArray.length()>0){
                JSONObject  city= jsonArray.getJSONObject(0);
               String id = city.getString("id");
               return id;
            }


        } catch (JSONException e) {
            throw new RuntimeException(e);
        }



        return null;
    }

}
